package gui;


import java.awt.Dimension;

import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.Filter;
import org.jdesktop.swingx.decorator.FilterPipeline;
import org.jdesktop.swingx.decorator.PatternFilter;

import database.DatabaseManager;
import database.Package;
import database.ReleaseIdentifier;



/**
 * @author aioros
 * 
 * JXTable packages (singleton)
 */
public class PackagesJXTable extends JXTable {

	/*-------------------- Fields ------------------------*/
	
	private static final long serialVersionUID = 1778890L;
	
	/**
	 * the single object PackagesJXTable
	 */
	private static PackagesJXTable packagesJXTable = new PackagesJXTable();
	
	/**
	 * search Filter
	 */
	private String searchFilter = ".*";
	
	/**
	 * stat Filter
	 */
	private String installFilter = "not";
	
	/**
	 * category Filter
	 */
	private String categoryFilter = ".*";
	
	/**
	 * release Identifier
	 */
	private ReleaseIdentifier currentRls;
	
	
	
	/*-------------------- Constructor ------------------------*/


	/**
	 * the constructor is private in order to create a singleton
	 */
	private PackagesJXTable() {
		super();
		
		this.initialize();
		this.selectedRows();
	}
	
	
	
	/*-------------------- Methodes ------------------------*/
	
	/**
	 * initialize the properties JXTable which contains release list
	 */
	public void initialize() {
		
		this.setModel(new PackagesTableModel());
		this.setPreferredScrollableViewportSize(new Dimension(1000, 300));
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setHorizontalScrollEnabled(true);
		
		TableColumn column = null;
		for (int i = 0; i < this.getColumnCount() - 1; i++) {
		    column = this.getColumnModel().getColumn(i);
		    switch(i){
		    
		    	case 0:
		    		column.setPreferredWidth(30);
		    		break;
		    	case 1:
		    		column.setPreferredWidth(30);
		    		break;
		    	case 2:
		    		column.setPreferredWidth(300);
		    		break;
		    	case 3:
		    		column.setPreferredWidth(30);
		    		break;
		    	case 4:
		    		column.setPreferredWidth(10);
		    		break;
		    	case 5:
		    		column.setPreferredWidth(30);
		    		break;
		    	case 6:
		    		column.setPreferredWidth(30);
		    		break;
		    	case 7:
		    		column.setPreferredWidth(30);
		    		break;
		    	default:
		    		column.setPreferredWidth(50);
		    }
		}
	}
	
	
	/**
	 * filter rows in the table
	 */
	private void filterRows(){
		
		int colName = PackagesJXTable.getPackagesJXTable().getColumn("Name").getModelIndex();
		int colInstall = PackagesJXTable.getPackagesJXTable().getColumn("Install").getModelIndex();
		int colCategory = PackagesJXTable.getPackagesJXTable().getColumn("Category").getModelIndex();
		PackagesJXTable.getPackagesJXTable().setFilters(new FilterPipeline(
	        new Filter[] { 
	        		new PatternFilter(searchFilter,0,colName) ,
	        		new PatternFilter(installFilter,0,colInstall) ,
	        		new PatternFilter(categoryFilter,0,colCategory)
	        		}));
	}
	
	
	
	private void selectedRows(){
		
		// Ask to be notified of selection changes.
		ListSelectionModel rowSM = this.getSelectionModel();
		rowSM.addListSelectionListener(new ListSelectionListener() {
		    public void valueChanged(ListSelectionEvent e) {
		        //Ignore extra messages.
		        if (e.getValueIsAdjusting()) return;

		        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
		        if (lsm.isSelectionEmpty()) {
		            //no rows are selected
		        } else {
		            int selectedRow = lsm.getMinSelectionIndex();
		            //selectedRow is selected
		            PackagesJXTable.this.currentRls = (ReleaseIdentifier) PackagesJXTable.this.getValueAt(selectedRow, 0);
		            Package p = DatabaseManager.getDatabaseManager().getLocalDatabase().getPackages().get(PackagesJXTable.this.currentRls.getPackageID());
		            String descriptionHTML = p.getDescriptionHTML(PackagesJXTable.this.currentRls.getBranchID(), PackagesJXTable.this.currentRls.getReleaseID());
		            DescriptionJLabel.getDescriptionJLabel().setText(descriptionHTML);
		        }
		    }
		});
	}
	
	
	
	/**
	 * @return the single object PackagesJXTable
	 */
	public static PackagesJXTable getPackagesJXTable(){
		
		return packagesJXTable;
	}


	public String getCategoryFilter() {
		return categoryFilter;
	}


	public void setCategoryFilter(String categoryFilter) {
		this.categoryFilter = categoryFilter;
		this.filterRows();
	}


	public String getSearchFilter() {
		return searchFilter;
	}


	public void setSearchFilter(String searchFilter) {
		this.searchFilter = searchFilter;
		this.filterRows();
	}


	public String getInstallFilter() {
		return installFilter;
	}


	public void setInstallFilter(String installFilter) {
		this.installFilter = installFilter;
		this.filterRows();
	}



	public ReleaseIdentifier getCurrentRelease() {
		return currentRls;
	}
	
}
